package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWaterProductionEquipmentRunningMapper;
import com.guofen.pojo.SjqWaterProductionDeliverWater;
import com.guofen.pojo.SjqWaterProductionEquipmentRunning;
import com.guofen.service.sjq.SjqWaterProductionEquipmentRunningService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqWaterProductionEquipmentRunningServiceImpl implements SjqWaterProductionEquipmentRunningService{

    @Autowired
    private SjqWaterProductionEquipmentRunningMapper sjqWaterProductionEquipmentRunningMapper;


    @Override
    public boolean addForm(double pump_pressure_1, double pump_pressure_2, double pump_pressure_3, double fan_frequency_1, double fan_frequency_2, String operator) {
        //获取当前日期
        String date = DateAndTimeUtil.getDate();

        SjqWaterProductionEquipmentRunning sjqWaterProductionEquipmentRunning = new SjqWaterProductionEquipmentRunning();
        sjqWaterProductionEquipmentRunning.setDate(date);
        sjqWaterProductionEquipmentRunning.setPumpPressure1(pump_pressure_1);
        sjqWaterProductionEquipmentRunning.setPumpPressure1IsExam("否");
        sjqWaterProductionEquipmentRunning.setPumpPressure2(pump_pressure_2);
        sjqWaterProductionEquipmentRunning.setPumpPressure2IsExam("否");
        sjqWaterProductionEquipmentRunning.setPumpPressure3(pump_pressure_3);
        sjqWaterProductionEquipmentRunning.setPumpPressure3IsExam("否");
        sjqWaterProductionEquipmentRunning.setFanFrequency1(fan_frequency_1);
        sjqWaterProductionEquipmentRunning.setFanFrequency1IsExam("否");
        sjqWaterProductionEquipmentRunning.setFanFrequency2(fan_frequency_2);
        sjqWaterProductionEquipmentRunning.setFanFrequency2IsExam("否");
        sjqWaterProductionEquipmentRunning.setOperator(operator);

        int result = sjqWaterProductionEquipmentRunningMapper.insert(sjqWaterProductionEquipmentRunning);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Boolean addRemarks(Integer Id, String pump_pressure_1_remark, String pump_pressure_2_remark,  String pump_pressure_3_remark,
                              String fan_frequency_1_remark, String fan_frequency_2_remark, String examiner){
        SjqWaterProductionEquipmentRunning sjqWaterProductionEquipmentRunning = new SjqWaterProductionEquipmentRunning();
        sjqWaterProductionEquipmentRunning.setId(Id);

        if(pump_pressure_1_remark != null)
            sjqWaterProductionEquipmentRunning.setPumpPressure1IsExam("是");

        if(pump_pressure_2_remark != null)
            sjqWaterProductionEquipmentRunning.setPumpPressure2IsExam("是");

        if(pump_pressure_3_remark != null)
            sjqWaterProductionEquipmentRunning.setPumpPressure3IsExam("是");

        if(fan_frequency_1_remark != null)
            sjqWaterProductionEquipmentRunning.setFanFrequency1IsExam("是");

        if(fan_frequency_2_remark != null)
            sjqWaterProductionEquipmentRunning.setFanFrequency2IsExam("是");

        sjqWaterProductionEquipmentRunning.setPumpPressure1Remark(pump_pressure_1_remark);
        sjqWaterProductionEquipmentRunning.setPumpPressure2Remark(pump_pressure_2_remark);
        sjqWaterProductionEquipmentRunning.setPumpPressure3Remark(pump_pressure_3_remark);
        sjqWaterProductionEquipmentRunning.setFanFrequency1Remark(fan_frequency_1_remark);
        sjqWaterProductionEquipmentRunning.setFanFrequency2Remark(fan_frequency_2_remark);

        sjqWaterProductionEquipmentRunning.setExaminer(examiner);
        int result = sjqWaterProductionEquipmentRunningMapper.updateByPrimaryKeySelective(sjqWaterProductionEquipmentRunning);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqWaterProductionEquipmentRunning findForm() {
        //获取当前日期
//        String date = DateAndTimeUtil.getDate();
//        System.out.println("当前日期" + date);
//        //获取最新记录
//        List<SjqWaterProductionEquipmentRunning> list = sjqWaterProductionEquipmentRunningMapper.findLast();
//        System.out.println("最新记录" + sjqWaterProductionEquipmentRunningMapper.findLast());
//        //获取最新记录日期
//        String dateLast = new String();
//        for(SjqWaterProductionEquipmentRunning sjqWaterProductionEquipmentRunning:list){
//            dateLast = sjqWaterProductionEquipmentRunning.getDate();
//            System.out.println("最新记录日期" + dateLast);
//        }
//        if (date.equals(dateLast))
//            return list;
//        else
//            return null;
        List<SjqWaterProductionEquipmentRunning> returnList = sjqWaterProductionEquipmentRunningMapper.findLast();
        int length = returnList.size();
        return returnList.get(length-1);
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWaterProductionEquipmentRunning> list = sjqWaterProductionEquipmentRunningMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWaterProductionEquipmentRunning> list = sjqWaterProductionEquipmentRunningMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWaterProductionEquipmentRunningMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWaterProductionEquipmentRunning record) {
        return sjqWaterProductionEquipmentRunningMapper.updateByPrimaryKeySelective(record);
    }

}
